// pages/topicality/chuxingtraffic/chuxingtraffic.js
const app = getApp()
const util = require("../../../utils/util.js");
var QQMapWX = require('../../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
  key: 'KR7BZ-XSQ3P-VGFD7-LNRZL-E7PXE-STF76'
});
Page({

  /**
   * 页面的初始数据
   */
  data: {
    city: '深圳',
    cityCode: 'sz',
    cityLv: '低风险',
    classLv: 'low',
    differentTitle: [], //异地出行title数组
    differentContent: {}, //异地出行内容
    curDiffIndex: 0,
    curDiffTitle: '',
    localTitle: [], //本地出行title数组
    localContent: {}, //本地出行title数组
    curLocalIndex: 0,
    curLocalTitle: '',
    daofen: '',
    bannerArr: [],
    ckey: ['进', '出', '入', '回', '离', '到', '来', '返', '去'], //标红关键字
    maxLength: 3, //最多显示几条
    hideMoreBtn: false //隐藏more btn
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    util.getUrl(app.globalData.url).then(e => {
      this.getLocation()
      this.renderContent(this.data.cityCode)
    })
  },
  // 确定选择城市
  chooseCity(e) {
    this.setData({
      city: e.detail.city,
      cityCode: e.detail.cityCode
    })
    this.renderContent(this.data.cityCode)
  },
  // 获取定位
  getLocation() {
    qqmapsdk.reverseGeocoder({
      success: (res) => {
        let city = res.result.address_component.city;
        if (city && !city.includes(this.data.city)) {
          wx.showModal({
            content: '您当前定位城市为' + city + '\n是否需要切换到' + city + '出行交通查询？',
            success: async res => {
              if (res.confirm) {
                city = city.substr(0, city.length - 1);
                const cityCode = await this.getCityCode(city)
                this.setData({
                  city,
                  cityCode
                })
                this.renderContent(this.data.cityCode)
              }
            }
          })
        }
      }
    })
  },
  // 获取cityCode,参数为市
  async getCityCode(city) {
    const res = await util.request("citycode.php", {
      action: "city",
      city: city
    }, "POST")

    return res.data.data[0].citycode
  },
  // 更多按钮
  more() {
    this.setData({
      maxLength: 100,
      hideMoreBtn: true
    })
  },
  // 风险等级
  renderLv(cityCode) {
    util.request('zhuanti.php', {
      action: 'fengxiandengji',
      citycode: cityCode || this.data.cityCode
    }, "POST").then((e) => {
      this.setData({
        cityLv: e.data.data.dengji.level,
        classLv: e.data.data.dengji.word
      })
    })
  },
  // 渲染内容
  renderContent(cityCode) {
    util.request('zhuanti.php', {
      action: 'jinchuzhengce',
      citycode: cityCode || this.data.cityCode
    }, 'POST').then((e) => {
      this.renderLv(cityCode)
      this.getBanners(cityCode)
      this.selectComponent("#fangyiTools").getTools(cityCode)
      
      this.setData({
        differentContent: e.data.data.yidi.list||[],
        differentTitle: e.data.data.yidi.title||[],
        curDiffTitle: e.data.data.yidi.title[0]||0,
        localContent: e.data.data.bendi.list||[],
        localTitle: e.data.data.bendi.title||[],
        curLocalTitle: e.data.data.bendi.title[0]||0,
        daofen: e.data.data.daofen.content||''
      })
    })
  },
  // 异地出行选择tab
  chooseDiffTab(e) {
    if (this.data.curDiffIndex == e.currentTarget.dataset.index) {
      return
    }
    this.setData({
      curDiffIndex: e.currentTarget.dataset.index,
      curDiffTitle: this.data.differentTitle[e.currentTarget.dataset.index],
      hideMoreBtn: false,
      maxLength: 3
    })
  },
  // 本地出行选择tab
  chooseLocalTab(e) {
    if (this.data.curLocalIndex == e.currentTarget.dataset.index) {
      return
    }
    this.setData({
      curLocalIndex: e.currentTarget.dataset.index,
      curLocalTitle: this.data.localTitle[e.currentTarget.dataset.index],
    })
  },

  // 分享按钮
  shareBtn(e) {
    this.setData({
      showModalStatus: true,
      openShare: false,
    })
  },
  openShare() {
    this.setData({
      openShare: true,
    });
  },
  closeShare() {
    this.setData({
      openShare: false,
    })
  },
  // 跳转主页
  toHome() {
    wx.reLaunch({
      url: '/pages/index/index?city=' + this.data.cityCode,
    })
  },
  // 文章跳转
  articleDetail(e) {
    let data = util.getValue("key", e);
    if (data) {
        if (data.id)
            wx.navigateTo({
                url: `/pages/article/detail/detail?id=${data.id}&city=${data.citycode}&type=${data.type}`,
            });
        else {
            util.navContact(this.data.detailInfo.title, data.url);
        }
    }
},
  // 获取轮播banner
  getBanners(citycode) {
    util.request('citycode.php', {
      action: 'lunbo',
      citycode: citycode || this.data.citycode
    }, 'POST').then(e => {
      this.setData({
        bannerArr: e.data.data
      })
    })
  },
  //banner详情
  toBannerDetail(e) {
    let path = e.currentTarget.dataset.path;
    let linkname = e.currentTarget.dataset.title;
    let appid = e.currentTarget.dataset.appid || '';
    if (path) {
      if (path.indexOf("pages") == -1 && path.indexOf("mp.weixin.qq.com") == -1 && !appid) {
        util.navContact(linkname, path)
      } else {
        util.navMethod(path, appid);
      }
    } else {
      wx.showToast({
        title: '还没有这个入口哦~',
        icon: 'none'
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '出行交通政策查询'
    }
  }
})